package com.jerkin.zookeeper;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

import java.util.Objects;

/**
 * @version V1.0
 * @ClassName: ZKFactory
 * @Description:
 * @author: huhongzhi
 * @date: 2019/12/16 16:43
 */
public class ZKFactory {

    private static final String zk_url = "127.0.0.1:2181";
    private static CuratorFramework curatorFramework = null;


    public static CuratorFramework createZK() {
        if (Objects.isNull(curatorFramework)) {
            System.out.println("进行ZK链接创建");
            create();
        }
//        curatorFramework.start();
        return curatorFramework;
    }

    private static CuratorFramework create() {
        ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, 3, Integer.MAX_VALUE);

        curatorFramework = CuratorFrameworkFactory.builder()
                .connectString(zk_url)
                .retryPolicy(retryPolicy)
                .sessionTimeoutMs(60000)
                .connectionTimeoutMs(15000)
                .namespace("test_node")
                .build();
        curatorFramework.start();
        return curatorFramework;
    }
}
